<?php
/*
//================================
//ProjectName:ZerSurl
//================================
//Master:Cig
//================================
*/
class Zer{
    //转换URl成员
	public $url;
	//数据库状态
	public $mysql_status;
	//跳转成员$_GET['u']
	public $u;
	
	//连接数据库(构造函数)
	public function __construct(){
		if(!$this->mysql_status=mysql_connect(DB_HOST,DB_USER,DB_PASS))die(mysql_error());
		mysql_select_db(DB_NAME);
	
	
	}
	
	//主方法
	public function Short(){
		
		$this->url=urlEncode($_GET['url']);
		$this->url=(mysql_real_escape_string($this->url));
	
		if(!$data_url=$this->Ver_data_url()){
			$b=$this->add_data_url();
			$data_arr=array(
			
				'status'=>1,
				'url_data'=>HOST_NAME.'?u='.urlDecode($b['u_code'])
			
			);
			returnAJax($data_arr);
		}else{
			$data_arr=array(
			
			'status'=>2,
			'url_data'=>HOST_NAME.'?u='.urlDecode($data_url['u_code'])
			
			);
			returnAjax($data_arr);

				
				}
	
	
	}

	//验证是否已经在数据库
	public function Ver_data_url(){
		$res=mysql_query("select * from url_data where url='".$this->url."'");
		if(is_array($arr=mysql_fetch_array($res))){
		return $arr;
		
		}else{
		return false;
		}
	
	
	}
	//添加到数据库
	public function add_data_url(){
		$data=array(
			'url'=>$this->url,
			
			'u_code'=>url_code($this->url)
		);
		$res="insert into url_data (url,u_code)values('".$data['url']."','".$data['u_code']."')";
		if(!mysql_query($res)){
			die(mysql_error());
		}else{
			return $data;
		}
	
	
	
	}
	
	
	
	
	
//跳转方法
	public function Redirect(){
		
		$this->u=mysql_real_escape_string(urlEncode($_GET['u']));
		$code_ar=$this->Redirect_model();
		if($code_ar){
		
echo '<meta http-equiv="Refresh" content="0; url='.urlDecode($code_ar['url']).'" /> ';
			
		
		}else{
			$error_arr=array(
			'status'=>'0',
			'data'=>'Error,Not data!',
			);
			returnAjax($error_arr,'zer',false);
		}
		
		
	}
	
//跳转方法数据库操作
	public function Redirect_model(){
		$query="select * from url_data where u_code='".$this->u."'";
		$arr=mysql_fetch_array(mysql_query($query));
		//var_dump($arr);
		if(is_array($arr)){
			//var_dump($arr);
			return $arr;
		
		}else{
			return false;
		}
	}
	
	
	
	
	
//	结束数据库
	public function __destructor(){
	
		mysql_close($this->status);
	
	}

}